package com.bosicen.crm.dao;

import java.util.List;

import org.springframework.stereotype.Component;

import com.bosicen.crm.dao.entity.Company;
import com.bosicen.crm.dao.entity.Conversion;
import com.bosicen.crm.dao.entity.Dealer;
import com.bosicen.crm.dao.entity.OrderNode;
import com.bosicen.crm.dao.entity.OrderProcess;
import com.bosicen.crm.dao.entity.ProductSery;
import com.bosicen.crm.dao.entity.ProductTable;
import com.bosicen.crm.dao.entity.ProductType;
import com.bosicen.crm.dao.entity.ProductUseFlag;
import com.bosicen.crm.tools.AppUtil;
/****
 * 公共信息查询DAO
 * @author Administrator
 *
 */
@Component("commonDao")
@SuppressWarnings("unchecked")
public class CommonDao extends HibernateDao<Object, Integer> {

	/****
	 * 查询产品系列列表
	 * @param companyid
	 * @return
	 */
	
	public List<ProductSery> findProductSery(int companyid){
		return this.getSession().createQuery(" from ProductSery where companyid=? order by id asc").setParameter(0, companyid).list();
	}
	/****
	 * 通过产品id查询所属系列信息
	 * @param productid
	 * @return
	 */
	public ProductSery findProductSeryByProductid(int productid){
		ProductSery ps=null;
		try{
			Object[] obj=(Object[]) this.getSession().createSQLQuery("select ps.id,ps.name,ps.pid,ps.companyid from product p ,product_series ps where ps.id=p.type and p.id="+productid).uniqueResult();
			if(obj!=null){
				ps=new ProductSery();
				ps.setId(AppUtil.ObjectToInt(obj[0]));
				ps.setName(AppUtil.ObjectToString(obj[1]));
				ps.setPid(AppUtil.ObjectToInt(obj[2]));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return ps;
	}
	
	/****
	 * 查询产品类型列表
	 * @param companyid
	 * @return
	 */
	public List<ProductType> findProductType(int companyid){
		return this.getSession().createQuery(" from ProductType where companyid=? order by id asc").setParameter(0, companyid).list();
	}
	
	/****
	 * 查询流程表列表
	 * @param companyid
	 * @return
	 */
	public List<OrderProcess> findOrderProcess(int companyid,int oid){
		return this.getSession().createQuery(" from OrderProcess where companyid=? and oid=? order by id asc").setParameter(0, companyid).setParameter(1, oid).list();
	}
	
	/****
	 * 查询产品环节列表
	 * @param companyid
	 * @return
	 */
	public List<OrderNode> findOrderNode(int companyid){
		return this.getSession().createQuery(" from OrderNode where companyid=? order by id asc").setParameter(0, companyid).list();
	}
	
	
	/****
	 * 查询经销商列表
	 * @param companyid
	 * @return
	 */
	public List<Dealer> findDealer(int companyid){
		return this.getSession().createQuery(" from Dealer where companyid=? order by id asc").setParameter(0, companyid).list();
	}
	
	/****
	 * 查询尺寸换算列表列表
	 * @param companyid
	 * @return
	 */
	public List<Conversion> findConversion(int companyid,int type){
		return this.getSession().createQuery(" from Conversion where companyid=? and type=? order by id asc").setParameter(0, companyid).setParameter(1, type).list();
	}
	/***
	 * 删除换算尺寸信息
	 * @param c
	 */
	public void DelConversion(Conversion c){
		this.getSession().createQuery("delete Conversion where companyid=? and type=? order by id asc").setParameter(0, c.getCompanyid()).setParameter(1, c.getType()).executeUpdate();
	}
	/****
	 * 查询注册企业信息
	 * @param companyid
	 * @return
	 */
	public List<Company> findCompany(int companyid){
		return this.getSession().createQuery(" from Company where companyid=? order by id asc").setParameter(0, companyid).list();
	}
	
	/****
	 * 查询注册企业信息
	 * @param companyid
	 * @return
	 */
	public List<Company> findCompanylist(){
		return this.getSession().createQuery(" from Company  order by id desc").list();
	}
	/***
	 * 根据类型查询线条/面板列表
	 * @param t
	 * @return
	 */
	public List<ProductTable> findProductTable(ProductTable t){
		return this.getSession().createQuery(" from ProductTable where flag=?  and companyid =? order by id asc").setParameter(0, t.getFlag()).setParameter(1, t.getCompanyid()).list();
	}
	/***
	 * 查询单个线条/面板详细信息
	 * @param id
	 * @return
	 */
	public ProductTable findProductTableInfo(int t){
		return (ProductTable) this.getSession().createQuery(" from ProductTable where id=? ").setParameter(0,t).uniqueResult();
	}
	/****
	 * 查询产品标志的列表
	 * @param id
	 * @return
	 */
	
	public List<ProductUseFlag> findProductUseFlagList(ProductUseFlag f){
		String sql = "from ProductUseFlag where companyid=? ";
		if(f.getFlag()>0){
			sql += " and flag="+f.getFlag();
		}
		sql += "  order by id asc ";
		return this.getSession().createQuery(sql).setParameter(0, f.getCompanyid()).list();
	}
}
